<?php

/**
 * 公告控制器
 *
 * @author haifeng.wu
 */

require_once 'CommonController.php';

class Admin_InfoController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->view->title = "管理中心";
        $this->_model = new Application_Model_Info();
        $this->username = Application_Model_UserLogin::getInstance()->getLoginedUserName();
    }

    /**
     * 公告列表
     * @author wuhaifeng
     * @return void
     */
    public function listAction(){
        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }
        
        //设置URL模板
        $urls = array();
        $wheres = array();
        
        $title = trim($this->_request->getPost('title'));//关键字
        $this->view->title  = $title;
        if (!empty($title)) {;
            $wheres[] = " `title` LIKE '%" . addslashes($title) . "%'";
        }
        
        
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = ' WHERE 1 ';
        $where .= count($wheres) > 0 ? ' AND ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_model->getTableName()}` " . $where . " ORDER BY id DESC";
        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();
        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
            
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();        
        $this->view->rows = $dbPaginator->getRows();
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
    }
    /**
    * 插入
    * @author wuhaifeng
    * @return void
    */
    public function insertAction()
    {
        if ($this->_request->isPost()) {
            $row = array();
            $filter = new Zend_Filter_StripTags();
            $data['class'] = trim($this->_request->getPost('class'));
            $data['title'] = $filter->filter(trim($this->_request->getPost('title')));
            if (empty($data['title'])) {
                echo $this->view->message('标题不能为空，请重新填写！') ;
                exit;
            }
            $data['addTime'] = date('Y-m-d H:i:s');
            $data['addUser'] = $this->username;
            $this->_model->insert($data);
            echo $this->view->message('操作成功！', $this->view->projectUrl(array('action' => 'list'))) ;
            exit;
        }
    }
    /**
    * 更新
    * @author wuhaifeng
    * @return void
    */
    public function updateAction()
    {
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             echo $this->view->message('请正确操作', $this->view->projectUrl(array('action' => 'list'))) ;
             exit;
        }
        $rows = $this->_model->filterData(array('id' => $id));
        if (empty($rows)) {
             echo $this->view->message('记录不存在', $this->view->projectUrl(array('action' => 'list'))) ;
             exit;
        }
        if ($this->_request->isPost()) {
            $filter = new Zend_Filter_StripTags();
            $data['class'] = trim($this->_request->getPost('class'));
            $data['title'] = $filter->filter(trim($this->_request->getPost('title')));
            if (empty($data['title'])) {
                echo $this->view->message('标题不能为空，请重新填写！') ;
                exit;
            }
            $this->_model->update($data, " `id` = '{$id}' ");
            echo $this->view->message('操作成功！', $this->view->projectUrl(array('action' => 'list'))) ;
            exit;
        }
        $this->view->rows = $rows;
    }
    /**
     * 删除动作
     * 
     * @return void
     */
    public function deleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                $this->_model->deleteById($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    

    
    
}